<template>
  <iLayout left>
    <section slot="title">小组管理</section>
    <section slot="operation">
      <div class="btns">
        <el-button round type="primary" @click="addGroupClick">新建小组</el-button>
      </div>
    </section>
    <!-- 小组列表 -->
    <section slot="left">
      <el-table :data="groups_list" @row-click="rowClick" :highlight-current-row="true" :stripe="true" style="width: 100%">
        <el-table-column prop="name" label="已有小组"> </el-table-column>
        <el-table-column width="100px">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="setGroupClick(scope.row)">修改</el-button>
            <el-button type="text" size="small" @click.stop="delGroupClick(scope.row)">解散</el-button>
          </template>
        </el-table-column>
      </el-table>
    </section>
    <!-- 小组信息 -->
    <section slot="right">
      <div v-if="selectedId">
        <GroupForm :set="false" ref="lookGroup" :fromData="setGroupData"></GroupForm>
      </div>
      <div v-else>
        <el-empty description="← 选择小组"></el-empty>
      </div>
    </section>
    <!-- 对话框们 -->
    <section class="dialogs">
      <!-- 新建小组对话框 -->
      <el-dialog title="新建小组" :visible.sync="dialog.addGroup" width="50%" @close="clearData" destroy-on-close>
        <div class="h50vh">
          <GroupForm ref="addGroup" :fromData="addGroupData"></GroupForm>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog.addGroup = false">取 消</el-button>
          <el-button type="primary" @click="addGroupFun">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改小组对话框 -->
      <el-dialog title="修改小组" :visible.sync="dialog.setGroup" width="60%" @close="clearData" destroy-on-close>
        <div class="h50vh">
          <GroupForm ref="setGroup" :fromData="setGroupData"></GroupForm>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog.setGroup = false">取 消</el-button>
          <el-button type="primary" @click="setGroupFun">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 解散小组对话框 -->
      <el-dialog title="" :visible.sync="dialog.delGroup" width="50%">
        <div>确定解散这个小组吗</div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog.delGroup = false">取 消</el-button>
          <el-button type="primary" @click="delGroupFun">确 定</el-button>
        </span>
      </el-dialog>
    </section>
  </iLayout>
</template>

<script>
import GroupForm from './formComponent/group_form.vue'
import * as api from '../../api/private/group'
export default {
  data() {
    return {
      // 小组列表
      groups_list: [{ name: '固定数据小组' }],
      // 新增小组
      addGroupData: {
        // deputyGroupLeader: [],
        // deviceIdList: [],
        // groupLeader: '',
        // name: '',
        // remark: '',
        // userIdList: []
      },
      selectedId: '',
      // 修改小组
      setGroupData: {
        deputyGroupLeader: [],
        deviceIdList: [],
        groupLeader: '',
        name: '',
        remark: '',
        userIdList: []
      },
      // 对话框们
      dialog: {
        addGroup: false,
        setGroup: false,
        delGroup: false
      }
    }
  },
  components: {
    GroupForm
  },
  mounted() {
    this.getGroups()
  },
  methods: {
    // 获取小组列表
    async getGroups() {
      let res = await api.getGroupList()
      if (res.code == 0) {
        this.groups_list = res.data
      } else {
        this.$msg.error('获取项目列表失败')
      }
    },
    // 小组按下
    rowClick(row) {
      this.getGroupData(row.id)
    },
    // 获取小组信息
    async getGroupData(id) {
      // 设置选中 ID
      this.selectedId = id
      if (this.selectedId == '') return
      // 获取小组详情信息
      let res = await api.getGroupData({ id })
      if (res.code == 0) {
        this.setGroupData = res.data
        this.$nextTick(item => {
          this.setGroupForm(res.data, 'lookGroup')
        })
      } else {
        this.$msg.error('获取用户信息失败')
      }
    },
    // 通知修改表单改变
    setGroupForm(data, ref) {
      // 获取指定组件并加工数据为可用
      let catchArray = this.$refs[ref ? ref : 'lookGroup'].catchArray
      catchArray.groupLeaderData = {
        name: data.leaderName,
        id: data.groupLeader
      }
      catchArray.deputyGroupLeaderData = data.deputyGroupLeaderList
      catchArray.userIdListData = data.userList
      catchArray.deviceIdListData = data.deviceList
      catchArray.earlyWarningIdListData = data.earlyWarningList
    },
    // 新建小组按下
    addGroupClick() {
      this.dialog.addGroup = true
      // this.$router.push({ path: '/addGroup', query: { type: 'add' } })
    },
    // 新建小组函数
    async addGroupFun() {
      let req = this.addGroupData
      let bol = this.$refs.addGroup.validate()
      if (bol) {
        let res = await api.addGroup(req)
        if (res.code == 0) {
          this.dialog.addGroup = false
          this.getGroups()
          this.setGroupData = {}
        } else {
          this.$msg.error('新建小组失败')
        }
        // 初始化表单
        this.$refs.addGroup.resetFields()
      }
    },
    // 修改小组按下
    setGroupClick(row) {
      // this.$router.push({
      //   path: '/addGroup',
      //   query: { type: 'set', data: row }
      // })
      this.setGroupData = JSON.parse(JSON.stringify(row))
      this.dialog.setGroup = true
      this.$nextTick(item => {
        this.setGroupForm(this.setGroupData, 'setGroup')
      })
    },
    // 修改小组函数
    async setGroupFun() {
      let req = this.setGroupData
      let bol = this.$refs.setGroup.validate()
      if (bol) {
        let res = await api.setGroup(req)
        if (res.code == 0) {
          this.dialog.setGroup = false
          this.getGroups()
          this.getGroupData(this.selectedId)
          this.setGroupData = {}
        } else {
          this.$msg.error('新建小组失败')
        }
        // 初始化表单
        this.$refs.setGroup.resetFields()
      }
    },
    // 解散小组按下
    delGroupClick(row) {
      this._catch = row
      this.dialog.delGroup = true
    },
    // 解散小组函数
    async delGroupFun() {
      let req = this._catch.id
      let res = await api.delGroup([req])
      if (res.code == 0) {
        this.$msg.success(`解散小组成功`)
        this.getGroups()
      } else {
        this.$msg.error('解散小组失败')
      }
      this.dialog.delGroup = false
    },
    clearData() {
      this.addGroupData = {}
      this.setGroupData = {}
    }
  }
}
</script>

<style>
</style>